package com.huazhi.wzsnw.reaml;

import com.huazhi.wzsnw.model.Menu;
import com.huazhi.wzsnw.model.Role;
import com.huazhi.wzsnw.model.User;
import com.huazhi.wzsnw.service.MenuService;
import com.huazhi.wzsnw.service.RoleMenuService;
import com.huazhi.wzsnw.service.RoleService;
import com.huazhi.wzsnw.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class LoginRealm extends AuthorizingRealm{
    @Autowired
    private UserService userService;
//    @Autowired
//    private MemberService memberService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private MenuService menuService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        User user = (User)principals.getPrimaryPrincipal();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Set<String> roles=new HashSet<>();
        List<Role> roleList = roleService.getRolesByUserTid(user.getTid());
        for(Role role:roleList) {
            roles.add(role.getRolename());
        }
        info.addRoles(roles);

        Set<String> permissions = new HashSet<String>();
        if(roleList.size()!=0){
            for(Role role:roleList){
                List<String> roleMenus=roleMenuService.getMenuTidByRoleTid(role.getTid());
                if(roleMenus.size()!=0){
                    List<Menu> menuList = menuService.findAll();
                    for (Menu e : menuList) {
                        if(!"3".equalsIgnoreCase(e.getType())){

                        }else {
                            boolean flag=roleMenus.contains(e.getTid());
                            if(flag){
                                permissions.add(e.getPerms());
                            }
                        }
                    }
                }
            }
        }
        //添加权限
        info.setStringPermissions(permissions);
        return info;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken autToken) throws AuthenticationException {
        UsernamePasswordToken userPwdToken = (UsernamePasswordToken) autToken;
        String userName = userPwdToken.getUsername();
        System.out.println("----------认证-----------------username="+userName);
        // 根据用户名查询数据库中用户的账号密码
        User user = userService.findByField("username",userName);
        if (user ==null) {
            System.out.println("未知账号");
            throw new UnknownAccountException();
        }
        if(user.getState().equalsIgnoreCase("0")){
            throw new LockedAccountException("此用户已被禁用");
        }
        String pass=user.getPassword();
        if(pass != null) {
            System.out.println("用户加密后的密码"+user.getPassword()+"==============");
        }
        SimpleAuthenticationInfo authenticationInfo =
                new SimpleAuthenticationInfo(user,pass,ByteSource.Util.bytes(user.getSalt()),getName());
        return authenticationInfo;
        /*Member member = memberService.findByField("username",userName);
        if (user ==null && member == null) {
            System.out.println("未知账号");
            throw new UnknownAccountException();
        }
        if(user != null && user.getState().equalsIgnoreCase("0")){
            throw new LockedAccountException("此用户已被禁用");
        }
        if(member != null && member.getIschecked().equalsIgnoreCase("0")){
            throw new LockedAccountException("此用户还未通过审核");
        }
        SimpleAuthenticationInfo authenticationInfo = null;
        String pass=user.getPassword();
        String memberpass = member.getPassword();
        if(pass != null) {
            System.out.println("用户加密后的密码"+user.getPassword()+"==============");
            authenticationInfo =
                    new SimpleAuthenticationInfo(user,pass,ByteSource.Util.bytes(user.getSalt()),getName());
        }else if(memberpass != null){
            System.out.println("会员用户加密后的密码"+memberpass+"==============");
            authenticationInfo =
                    new SimpleAuthenticationInfo(member,memberpass,ByteSource.Util.bytes(member.getSalt()),getName());
        }
        return authenticationInfo;*/

    }

    @Override
    public  boolean isPermitted(PrincipalCollection principals, String permission){
        User user = (User)principals.getPrimaryPrincipal();
        return user.getIssuper().equalsIgnoreCase("0")||super.isPermitted(principals,permission);
    }
    @Override
    public boolean hasRole(PrincipalCollection principals, String roleIdentifier) {
        User user = (User)principals.getPrimaryPrincipal();
        return user.getIssuper().equalsIgnoreCase("0")||super.hasRole(principals,roleIdentifier);
    }



}
